package com.ran.java_gobang.api;

import com.ran.java_gobang.game.request.UserLoginRequest;
import com.ran.java_gobang.game.request.UserRegisterRequest;
import com.ran.java_gobang.model.User;
import com.ran.java_gobang.serivce.UserService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ran
 * Date: 2025-08-27
 * Time: 21:18
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserAPI {

    @Autowired
    private UserService userService;

    @ResponseBody // 返回json
    @RequestMapping("/login")
    public Object login(@RequestBody  @Validated UserLoginRequest userLoginRequest, HttpServletRequest request) {
        log.info("登录的用户名: " + userLoginRequest.getUserName());
        // 根据userName去数据库查找
        return userService.login(userLoginRequest,request);
    }

    /**
     * 注册用户
     * @param
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/register")
    public Object register(@RequestBody @Validated UserRegisterRequest userRegisterRequest) {
        log.info("用户注册: 用户名: " + userRegisterRequest.getUserName() + " ,密码: " + userRegisterRequest.getPassword());
        return userService.register(userRegisterRequest);
    }

    /**
     * 获取当前玩家信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/getUserInfo")
    public Object getUserInfo(HttpServletRequest request) {
        return userService.getUserInfo(request);
    }
}
